//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    orderNum: {},
    orderData: [],
    showType: '待支付',
    pageIndex: 1,
    state: '',
    active: true,
    fiveData: [{
        'title': '待付款',
        'active': true,
        'src0': "../../images/icon/order_1.png",
        'src1': "../../images/icon/order1.png",
        'showType': "待支付"
      },
      {
        'title': '待发货',
        'active': false,
        'src0': "../../images/icon/order_2.png",
        'src1': "../../images/icon/order2.png",
        'showType': "待发货"
      },
      {
        'title': '已发货',
        'active': false,
        'src0': "../../images/icon/order_3.png",
        'src1': "../../images/icon/order3.png",
        'showType': "待收货"
      },
      {
        'title': '退款售后',
        'active': false,
        'src0': "../../images/icon/order_4.png",
        'src1': "../../images/icon/order4.png",
        'showType': "退货中"
      },
      {
        'title': '交易完',
        'active': false,
        'src0': "../../images/icon/order_5.png",
        'src1': "../../images/icon/order5.png",
        'showType': "交易结束"
      }

    ],
    // 
    uid: "",
    userType: "",
    // 修改邮费
    orderNumber: "",
    youFeeFlag: true,
    youFee: "",
    youFeeNew: "",
    // 发货
    faHuoFlag: true,
    kDCompany: "",
    kDDanHao: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })
    // this.GetOrderStateNum();
    // this.GetOrderList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.GetOrderList();
  },

  // 
  showTypeTap: function(e) {
    // console.log(e);
    var fiveData = this.data.fiveData;
    var showType = e.currentTarget.dataset.showtype;
    for (var i = 0, len = fiveData.length; i < len; ++i) {
      if (fiveData[i].showType == showType) {
        fiveData[i].active = true;
      } else {
        fiveData[i].active = false
      }
    }
    this.setData({
      fiveData: fiveData,
      showType: showType
    })
    this.data.pageIndex = 1;
    this.data.orderData = [];
    this.GetOrderList();
    // console.log(showType);
  },
  // 得到订单状态所有的个数
  GetOrderStateNum: function() {
    var that = this;
    var uid = this.data.uid;
    var orderNum = this.data.orderNum;
    var fiveData = this.data.fiveData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetBusinessOrderStateNum&UID=" + uid + "&Type=1"),
      success: function(res) {
        orderNum = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        fiveData[0].num = orderNum.waitPay;
        fiveData[1].num = orderNum.waitshipped;
        fiveData[2].num = orderNum.waitReceipt;
        fiveData[3].num = orderNum.returngoods;
        fiveData[4].num = orderNum.finished;
        that.setData({
          orderNum: orderNum,
          fiveData: fiveData,
        })
      }
    })
  },
  //加载订单列表
  GetOrderList: function() {
    var that = this;
    var uid = this.data.uid;
    var showType = this.data.showType;
    var pageIndex = this.data.pageIndex;
    var orderData = this.data.orderData;
    if (pageIndex == 1) {
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetOrderList&UID=" + uid + "&Type=2&DeliverGoods=0&PageSize=10&PageIndex=" + pageIndex + "&OrderState=" + showType),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        // console.log(json);
        for (var i = 0; i < json.length; i++) {
          orderData.push(json[i])
        }
        that.setData({
          orderData: orderData,
        })
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();
        // console.log(orderData);
        // console.log(baseJs.api + "method=GetOrderList&UID=" + uid+ "&Type=1&DeliverGoods=0&PageSize=10&PageIndex=" + pageIndex + "&OrderState=" + showType);
      }
    })
  },



  // 修改邮费
  updateMailFee: function(e) {
    var that = this;
    this.setData({
      youFee: e.currentTarget.dataset.youfee,
      orderNumber: e.currentTarget.dataset.ordernumber,
      youFeeFlag: false,
    })

  },
  youFeeCancel: function() {
    this.setData({
      youFeeFlag: true,
    })
  },
  youFeeInput: function(e) {
    this.setData({
      youFeeNew: e.detail.value
    })

  },
  youFeeConfirm: function() {
    var that = this;
    var youFeeNew = this.data.youFeeNew;
    var orderNumber = this.data.orderNumber;
    if (youFeeNew == '') {
      wx.showToast({
        title: "请输入新的邮费金额！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=ModifyMailOrderYoufei&OrderNumber=" + orderNumber + "&SJID=" + uid + "&YouFei=" + youFeeNew),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          that.data.pageIndex = 1;
          that.data.orderData = [];
          that.GetOrderList();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })

  },
  // 发货
  sendGoods: function(e) {
    this.setData({
      faHuoFlag: false,
      orderNumber: e.currentTarget.dataset.ordernumber,
    })

  },
  companyInput: function(e) {
    this.setData({
      kDCompany: e.detail.value,
    })

  },
  danHaoInput: function(e) {
    this.setData({
      kDDanHao: e.detail.value,
    })

  },
  faHuoCancel: function() {
    this.setData({
      faHuoFlag: true,
    })
  },
  faHuoConfirm: function() {
    var that = this;
    var kDCompany = this.data.kDCompany;
    var kDDanHao = this.data.kDDanHao;
    var fiveData = this.data.fiveData;
    var orderNumber = this.data.orderNumber;
    if (kDCompany == '') {
      wx.showToast({
        title: "请输入快递公司！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (kDDanHao == '') {
      wx.showToast({
        title: "请输入快递单号！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=SureDelivergoods&OrderNumber=" + orderNumber + "&ExpressNumber=" + kDDanHao + "&ExpressCompany=" + kDCompany),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          fiveData[1].active = true;
          that.setData({
            showType: "待发货",
            fiveData: fiveData,
            pageIndex: 1,
            orderData: [],
          })
          that.GetOrderStateNum();
          that.GetOrderList();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 删除订单
  delOrder: function(e) {
    var that = this;
    var fiveData = this.data.fiveData;
    wx.showModal({
      title: '提示',
      content: '确定退款?',
      cancelText: "取消",
      confirmText: "确定",
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=SureOrderRefund&OrderNumber=" + e.currentTarget.dataset.ordernumber),
            success: function (res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                fiveData[2].active = true;
                that.setData({
                  showType: "待收货",
                  fiveData: fiveData,
                  pageIndex: 1,
                  orderData: [],
                })
                that.GetOrderStateNum();
                that.GetOrderList();
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {

        }
      },
    })
  },
  // 同意退款
  agreeRefund: function(e) {
    var that = this;
    var uid = this.data.uid;
    var fiveData = this.data.fiveData;
    wx.showModal({
      title: '提示',
      content: '确定删除该订单?',
      cancelText: "取消",
      confirmText: "确定",
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=DeleteOrder&OrderNumber=" + e.currentTarget.dataset.ordernumber + "&UID=" + uid + "&IsBusinessOrder=0"),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                orderData.splice(e.currentTarget.dataset.index, 1);
                that.setData({
                  orderData: orderData
                })
                that.GetOrderStateNum();
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } 
      },
    })
  },
  // 
})